﻿using MaterialDesignThemes.Wpf;
using OpenTK.Graphics.ES20;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Task;
using 基于WPF的电缆采样数据分析系统.Service;
using 基于WPF的电缆采样数据分析系统.Tools.LangugeTool;
using 基于WPF的电缆采样数据分析系统.Tools.MessagePopupTool;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork
{
    public class ImportAndExportUCViewModel : BindableBase, IDialogHostAware
    {

        // 单例模型
        public static ImportAndExportUCViewModel Instance = new ImportAndExportUCViewModel();

        // 主机名
        private const string DialogHostName = "RootDialog";


        #region 确定
        /// <summary>
        /// 确定命令
        /// </summary>
        public DelegateCommand SaveCommand { get; set; }

        /// <summary>
        /// 确定方法
        /// </summary>
        private void SaveFun()
        {
            if (DialogHost.IsDialogOpen(DialogHostName))
            {

                // 保存选中的索引
                for (int i = 0; i < CurrentTaskList.Count; i++)
                {
                    if (CurrentTaskList[i].IsSelected)
                    {
                        IndexList.Add(CurrentTaskList[i].Tid);
                    }
                }

                // 未选中任务
                if (IndexList.Count == 0)
                {
                    MessagePopup.DisplayMessagePopup(LangugeConvertor.GetValue("IfTheTaskIsNotSelectedTheTaskCannotBeExported"));
                    return;
                }

                // 返回选中的索引集合
                DialogParameters paras = new DialogParameters();
                paras.Add("IndexList", IndexList);

                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.Yes, paras));
            }
        }
        #endregion


        #region 取消
        /// <summary>
        /// 取消命令
        /// </summary>
        public DelegateCommand CancelCommand { get; set; }

        /// <summary>
        /// 取消方法
        /// </summary>
        private void CancelFun()
        {
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
            }
        }
        #endregion


        /// <summary>
        /// 打开过程中
        /// </summary>
        /// <param name="parameters"></param>
        public void OnDialogOpening(IDialogParameters parameters)
        {
            // 清空当前任务集合
            CurrentTaskList.Clear();
            IndexList.Clear();

            // 将任务栏的任务集合赋值到导出任务弹窗中
            var currentTaskList = parameters.GetValue<List<TaskDataInfo>>("CurrentTaskList");

            // 渲染集合
            for (int i = 0; i < currentTaskList.Count; i++)
            {
                CurrentTaskList.Add(new TaskDataInfo()
                {
                    Tid = currentTaskList[i].Tid,
                    DescriptionInfo = currentTaskList[i].DescriptionInfo,
                    ChannelConfig = currentTaskList[i].ChannelConfig,
                    HorizontalArgument = currentTaskList[i].HorizontalArgument,
                    VerticalArgument = currentTaskList[i].VerticalArgument,
                    CreateUser = currentTaskList[i].CreateUser,
                    UpdateUser = currentTaskList[i].UpdateUser,
                    CreateTime = currentTaskList[i].CreateTime,
                    UpdateTime = currentTaskList[i].UpdateTime,
                    XAxisTranslation = currentTaskList[i].XAxisTranslation,
                    YAxisTranslation = currentTaskList[i].YAxisTranslation,
                    IsSelected = false
                });
            }
        }

        /// <summary>
        /// 构造器
        /// </summary>
        public ImportAndExportUCViewModel()
        {
            // 绑定确定和取消命令
            SaveCommand = new DelegateCommand(SaveFun);
            CancelCommand = new DelegateCommand(CancelFun);

        }


        #region 导出任务集合
        /// <summary>
        /// 当前任务集合
        /// </summary>
        private ObservableCollection<TaskDataInfo> _CurrentTaskList = new ObservableCollection<TaskDataInfo>();

        /// <summary>
		/// 当前任务集合
		/// </summary>
        public ObservableCollection<TaskDataInfo> CurrentTaskList
        {
            get { return _CurrentTaskList; }
            set
            {
                _CurrentTaskList = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 索引集合
        /// </summary>
        private List<int> _IndexList = new List<int>();

        /// <summary>
        /// 索引集合
        /// </summary>
        public List<int> IndexList
        {
            get { return _IndexList; }
            set { _IndexList = value; }
        }

        #endregion



    }
}
